<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category Image 
 * +------------------------------------------------------------+
 * 图像处理扩展类
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.meizong.net 2013
 * @version 1.0
 *
 * Modified at : 2013-2-25 15:50:11
 *
 */
class Image {
	/**
	 * 
	 * +------------------------------------------------------------+
	 * @name thumb
	 * +------------------------------------------------------------+
	 * 图片生成缩略图
	 * +------------------------------------------------------------+
	 * @example Image::thumb($src_file, $dst_file ,100 ,100)
	 *
	 * @param string $src_file	源图片路径
	 * @param string $dst_file	缩略图存储路径
	 * @param int $new_width	缩略图宽度
	 * @param int $new_height	缩略图高度
	 *
	 */
	static public function thumb($src_file, $dst_file, $new_width, $new_height) {
		$new_width = intval ( $new_width );
		$new_height = intval ( $new_height );
		if ($new_width < 1 || $new_height < 1) {
			throw new E('缩略图宽高设置有误');
		}
		if (! file_exists ( $src_file )) {
			throw new E('源图片不存在');
		}
		
		$infos = @getimagesize ( $src_file );
		if (!$infos){
			return false;
		}
		$type = $infos[2];
	
		$support_type = array (IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF );
		if (! in_array ( $type, $support_type, true )) {
			return false;
			//throw new E('图片格式只支持jpg、gif、png，当前图片格式为：'.$src_file);
		}
		//Load image 
		switch ($type) {
			case IMAGETYPE_JPEG :
				$src_img = imagecreatefromjpeg ( $src_file );
				break;
			case IMAGETYPE_PNG :
				$src_img = imagecreatefrompng ( $src_file );
				break;
			case IMAGETYPE_GIF :
				$src_img = imagecreatefromgif ( $src_file );
				break;
			case IMAGETYPE_BMP :
				$src_img = imagecreatefromwbmp( $src_file );
				break;
			default :
				return false;
				//throw new E('加载源图片出错');
		}
		$w = imagesx ( $src_img );
		$h = imagesy ( $src_img );
		$ratio_w = $new_width / $w;
		$ratio_h = $new_height / $h;
		$ratio = 1.0;
		
		// 生成的图像的高宽比原来的都小，或都大 ，原则是 取大比例放大，取大比例缩小（缩小的比例就比较小了） 
		if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
			if ($ratio_w < $ratio_h) {
				$ratio = $ratio_h; // 情况一，宽度的比例比高度方向的小，按照高度的比例标准来裁剪或放大 
			} else {
				$ratio = $ratio_w;
			}
			// 定义一个中间的临时图像，该图像的宽高比 正好满足目标要求 
			$inter_w = ( int ) ($new_width / $ratio);
			$inter_h = ( int ) ($new_height / $ratio);
			$inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
			
			$new_img = imagecreatetruecolor ( $new_width, $new_height );
			///处理裁剪后图片透明问题
			if($type == IMAGETYPE_PNG){
				imagealphablending($inter_img, false);
				imagesavealpha($inter_img,true);
				imagealphablending($new_img, false);
				imagesavealpha($new_img,true);
			}
			$src_x = ($w -  $inter_w) / 2;
			$src_y = ($h - $inter_h) / 2;
			imagecopy ( $inter_img, $src_img, 0, 0, $src_x, $src_y, $inter_w, $inter_h );
			imagecopyresampled ( $new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h );
			switch ($type) {
				case IMAGETYPE_JPEG :
					imagejpeg ( $new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'jpg'), 100 ); // 存储图像 
					break;
				case IMAGETYPE_PNG :
					imagepng ( $new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'png'), 9 );
					break;
				case IMAGETYPE_GIF :
					imagegif ( $new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'gif'), 100 );
					break;
				case IMAGETYPE_BMP :
					imagewbmp($new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'jpg'), 100);
					break;
				default :
					break;
			}
		} 
		else {
			$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; //取比例大的那个值 
			// 定义一个中间的大图像，该图像的高或宽和目标图像相等，然后对原图放大 
			$inter_w = ( int ) ($w * $ratio);
			$inter_h = ( int ) ($h * $ratio);
			$inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
			//将原图缩放比例后裁剪 
			
			// 定义一个新的图像 
			$new_img = imagecreatetruecolor ( $new_width, $new_height );
			
			if($type == IMAGETYPE_PNG){
				imagealphablending($inter_img, false);
				imagesavealpha($inter_img,true);
				imagealphablending($new_img, false);//取消默认的混色模式（为解决阴影为绿色的问题）
				imagesavealpha($new_img,true);//设定保存完整的 alpha 通道信息（为解决阴影为绿色的问题）
			}
			$src_x = ($inter_w -  $new_width) / 2;
			$src_y = ($inter_h - $new_height) / 2;
			imagecopyresampled ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h );
			imagecopy ( $new_img, $inter_img, 0, 0, $src_x, $src_y, $new_width, $new_height );
			switch ($type) {
				case IMAGETYPE_JPEG :
					imagejpeg ( $new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'jpg'), 100 ); // 存储图像 
					break;
				case IMAGETYPE_PNG :
					imagepng ( $new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'png'), 9 );
					break;
				case IMAGETYPE_GIF :
					imagegif ( $new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'gif'), 100 );
					break;
				case IMAGETYPE_BMP :
					imagewbmp($new_img, $real_dst_file = self::get_real_imagepath($dst_file, 'jpg'), 100);
					break;
				default :
					break;
			}
		} // if3 
		
		return $real_dst_file;
	}
	
	/**
	 * 根据图像媒体类型，返回正确的文件路径
	 * 
	 * @param string $imagepath
	 * @param string $type
	 * 
	 * @return string
	 */
	public static function get_real_imagepath($imagepath, $type = 'jpg'){
		$pathinfo = pathinfo($imagepath);
		$ext = strtolower($pathinfo['extension']);
		switch (strtolower($type)){
			case 'jpg':
				if ($ext != 'jpg' && $ext != 'jpeg'){
					$imagepath = $pathinfo['dirname'] . DS . $pathinfo['filename'] . '.jpg';
				}
				break;
			
			case 'gif':
				if ($ext != 'gif'){
					$imagepath = $pathinfo['dirname'] . DS . $pathinfo['filename'] . '.gif';
				}
				break;
				
			case 'png':
				if ($ext != 'png'){
					$imagepath = $pathinfo['dirname'] . DS . $pathinfo['filename'] . '.png';
				}
				break;
				
			case 'bmp':
				if ($ext != 'bmp'){
					$imagepath = $pathinfo['dirname'] . DS . $pathinfo['filename'] . '.bmp';
				}
				break;
		}
		
		return $imagepath;
	}
}

?>